import java.awt.Color;
import java.text.ParseException;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;

public class AssetForm extends JPanel {

	protected JFormattedTextField txtName;
	protected JFormattedTextField txtOwner;
	protected JFormattedTextField txtCustodian;
	protected JFormattedTextField txtStorageLocation;
	protected JComboBox<String> comboClassification;
	protected JComboBox<String> comboMaintenance;
	protected JComboBox<String> comboType;
	protected JComboBox<String> comboFinancial;
	protected JComboBox<String> comboConfidentiality;
	protected JComboBox<String> comboIntegrity;
	protected JComboBox<String> comboAvailability;
	
	protected DatePicker datePickerDateAcquired;
	protected DatePicker datePickerRetentionPeriod;
	
	//Other attributes
	protected ArrayList<CodeDescriptionPair> typeCDP;
	protected ArrayList<CodeDescriptionPair> maintenanceCDP;
	protected ArrayList<CodeDescriptionPair> classificationCDP;
	protected ArrayList<CodeDescriptionPair> financialCDP;
	protected ArrayList<CodeDescriptionPair> confidentialityCDP;
	protected ArrayList<CodeDescriptionPair> integrityCDP;
	protected ArrayList<CodeDescriptionPair> availabilityCDP;
	public static final String validCharsForNames = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz .-";
	
	public AssetForm()
	{
		initCDPs();
		initGUI();
	}
	
	private void initGUI()
	{
		//Auto-Generated GUI 
		setLayout(null);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(57, 65, 52, 14);
		add(lblName);
		
		JLabel lblOwner = new JLabel("Owner:");
		lblOwner.setBounds(57, 118, 52, 14);
		add(lblOwner);
		
		JLabel lblCustodian = new JLabel("Custodian:");
		lblCustodian.setBounds(57, 173, 72, 14);
		add(lblCustodian);
		
		JLabel lblType = new JLabel("Type:");
		lblType.setBounds(308, 65, 52, 14);
		add(lblType);
		
		JLabel lblDateAcquired = new JLabel("Date Acquired:");
		lblDateAcquired.setBounds(57, 286, 84, 14);
		add(lblDateAcquired);
		
		JLabel lblRetentionPeriod = new JLabel("Retention Period:");
		lblRetentionPeriod.setBounds(57, 341, 100, 14);
		add(lblRetentionPeriod);
		
		JLabel lblMaintenanceSchedule = new JLabel("Maintenance Schedule:");
		lblMaintenanceSchedule.setBounds(308, 96, 148, 14);
		add(lblMaintenanceSchedule);
		
		JLabel lblValue = new JLabel("Value:");
		lblValue.setBounds(390, 173, 55, 14);
		add(lblValue);
		
		JLabel lblFinancial = new JLabel("Financial:");
		lblFinancial.setBounds(306, 209, 73, 14);
		add(lblFinancial);
		
		JLabel lblConfidentiality = new JLabel("Confidentiality:");
		lblConfidentiality.setBounds(306, 239, 93, 14);
		add(lblConfidentiality);
		
		JLabel lblIntegrity = new JLabel("Integrity:");
		lblIntegrity.setBounds(306, 269, 73, 14);
		add(lblIntegrity);
		
		JLabel lblAvailability = new JLabel("Availability:");
		lblAvailability.setBounds(306, 299, 73, 14);
		add(lblAvailability);
		
		JLabel lblClassification = new JLabel("Classification:");
		lblClassification.setBounds(308, 127, 93, 14);
		add(lblClassification);
		
		comboClassification = new JComboBox<String>();
		comboClassification.setBounds(449, 127, 110, 20);
		add(comboClassification);
		
		JLabel lblStorageLocation = new JLabel("Storage Location:");
		lblStorageLocation.setBounds(57, 228, 105, 14);
		add(lblStorageLocation);
		
		comboMaintenance = new JComboBox<String>();
		comboMaintenance.setBounds(449, 96, 110, 20);
		add(comboMaintenance);
		
		comboType = new JComboBox<String>();
		comboType.setBounds(449, 65, 110, 20);
		add(comboType);
		
		//Formatters
		try {
			MaskFormatter assetNameFormatter = new MaskFormatter("******************************");
			
			MaskFormatter nameFormatter = new MaskFormatter("******************************");
			nameFormatter.setValidCharacters(validCharsForNames);
			
			MaskFormatter storageLocationFormatter = new MaskFormatter("******************************");
			
			
			txtName = new JFormattedTextField(assetNameFormatter);
			txtName.setBounds(57, 87, 195, 26);
			add(txtName);
			txtName.setColumns(10);
			
			txtOwner = new JFormattedTextField(nameFormatter);
			txtOwner.setBounds(57, 142, 200, 25);
			add(txtOwner);
			txtOwner.setColumns(10);
			
			txtCustodian = new JFormattedTextField(nameFormatter);
			txtCustodian.setBounds(57, 197, 200, 25);
			add(txtCustodian);
			txtCustodian.setColumns(10);
			
			txtStorageLocation = new JFormattedTextField(storageLocationFormatter);
			txtStorageLocation.setBounds(57, 252, 200, 25);
			add(txtStorageLocation);
			txtStorageLocation.setColumns(10);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		comboFinancial = new JComboBox<String>();
		comboFinancial.setBounds(449, 206, 110, 20);
		add(comboFinancial);
		
		comboConfidentiality = new JComboBox<String>();
		comboConfidentiality.setBounds(449, 236, 110, 20);
		add(comboConfidentiality);
		
		comboIntegrity = new JComboBox<String>();
		comboIntegrity.setBounds(449, 266, 110, 20);
		add(comboIntegrity);
		
		comboAvailability = new JComboBox<String>();
		comboAvailability.setBounds(449, 296, 110, 20);
		add(comboAvailability);
		
		//User-defined GUI initializations
		addDescriptionsToComboBox(typeCDP, comboType);
		addDescriptionsToComboBox(maintenanceCDP, comboMaintenance);
		addDescriptionsToComboBox(classificationCDP, comboClassification);
		addDescriptionsToComboBox(financialCDP, comboFinancial);
		addDescriptionsToComboBox(confidentialityCDP, comboConfidentiality);
		addDescriptionsToComboBox(integrityCDP, comboIntegrity);
		addDescriptionsToComboBox(availabilityCDP, comboAvailability);
		
		datePickerDateAcquired = new DatePicker(168, 254);
		datePickerDateAcquired.setSize(203, 20);
		datePickerDateAcquired.setLocation(57, 310); //upper left (x,y) of the panel. auto-generated
		
		datePickerRetentionPeriod = new DatePicker(168, 279);
		datePickerRetentionPeriod.setLocation(57, 365);

		add(datePickerDateAcquired);
		add(datePickerRetentionPeriod);	
		
		
	}
		
	private void initCDPs()
	{
		typeCDP = CodeDescriptionPair.getCodeDescriptionPairsFor(CodeDescriptionPair.TABLE_TYPE);
		maintenanceCDP = CodeDescriptionPair.getCodeDescriptionPairsFor(CodeDescriptionPair.TABLE_MINTENANCE);
		classificationCDP = CodeDescriptionPair.getCodeDescriptionPairsFor(CodeDescriptionPair.TABLE_CLASSIFICATION);
		financialCDP = CodeDescriptionPair.getCodeDescriptionPairsFor(CodeDescriptionPair.TABLE_FINANCIAL);
		confidentialityCDP = CodeDescriptionPair.getCodeDescriptionPairsFor(CodeDescriptionPair.TABLE_CONFIDENTIALITY);
		integrityCDP = CodeDescriptionPair.getCodeDescriptionPairsFor(CodeDescriptionPair.TABLE_INTEGRITY);
		availabilityCDP = CodeDescriptionPair.getCodeDescriptionPairsFor(CodeDescriptionPair.TABLE_AVAILABILITY);
	}

	private void addDescriptionsToComboBox(ArrayList<CodeDescriptionPair> codeDescriptionPairs, JComboBox<String> comboBox)
	{
		for(int i=0;i<codeDescriptionPairs.size(); i++)
			comboBox.addItem(codeDescriptionPairs.get(i).getDescription());
	}
	
	protected void clearForm()
	{
		txtName.setText("");
		txtOwner.setText("");
		txtCustodian.setText("");
		txtStorageLocation.setText("");
		
		txtName.setBackground(Color.WHITE);
		txtOwner.setBackground(Color.WHITE);
		txtCustodian.setBackground(Color.WHITE);
		txtStorageLocation.setBackground(Color.WHITE);
	}
}
